/* eslint-disable */

// 构建树
function buildTree(data, parentId = null, idKey = 'id', parentIdKey = 'parentId', childrenKey = 'children') {
  const tree = [];

  // 遍历数组数据
  for (let i = 0; i < data.length; i++) {
    const node = data[i];

    // 判断当前节点的parentId是否与指定的parentId相等
    if (node[parentIdKey] === parentId) {
      // 递归构建子树
      const children = buildTree(data, node[idKey], idKey, parentIdKey, childrenKey);

      // 将子树添加到当前节点的children属性中
      if (children.length > 0) {
        node[childrenKey] = children;
      }

      // 将当前节点添加到树中
      tree.push(node);
    }
  }
  return tree;
}

// 扁平化
function flattenTree(tree, childrenKey = 'children') {
  const result = [];

  // 遍历树结构
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];

    // 将当前节点添加到结果数组中
    result.push(node);

    // 判断当前节点是否有子节点
    if (node[childrenKey] && node[childrenKey].length > 0) {
      // 递归扁平化子节点
      const children = flattenTree(node[childrenKey], childrenKey);

      // 将扁平化后的子节点添加到结果数组中
      result.push(...children);
    }
  }

  return result;
}

// 查询树
function searchTree(tree, property, value, childrenKey = 'children') {
  const result = [];

  // 遍历树结构
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];

    // 判断当前节点的属性值是否与目标值相等
    if (node[property] === value) {
      // 将匹配的节点添加到结果数组中
      result.push(node);
    }

    // 判断当前节点是否有子节点
    if (node[childrenKey] && node[childrenKey].length > 0) {
      // 递归搜索子节点
      const matches = searchTree(node[childrenKey], property, value, childrenKey);

      // 将匹配的子节点添加到结果数组中
      result.push(...matches);
    }
  }

  return result;
}

// 一维数组插入树
function insertIntoTree(tree, data, parentIdKey = 'parentId', childrenKey = 'children') {
  // 创建一个映射表，用来存储每个节点的子节点
  const map = {};

  // 遍历树结构，将每个节点的子节点存储到映射表中
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    const parentId = node.id;

    if (!map[parentId]) {
      map[parentId] = [];
    }

    map[parentId].push(node);
  }

  // 将数据插入到树结构中
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const parentId = node[parentIdKey];

    if (map[parentId]) {
      map[parentId].push(node);
    }
  }

  // 返回新的树结构数据
  return tree;
}

// 树插入树
function insertTreeIntoTree(targetTree, sourceTree, parentIdKey = 'parentId', childrenKey = 'children') {
  // 创建一个映射表，用来存储每个节点的子节点
  const map = {};

  // 遍历目标树结构，将每个节点的子节点存储到映射表中
  for (let i = 0; i < targetTree.length; i++) {
    const node = targetTree[i];
    const parentId = node.id;

    if (!map[parentId]) {
      map[parentId] = [];
    }

    map[parentId].push(node);
  }

  // 将源树结构插入到目标树结构中
  for (let i = 0; i < sourceTree.length; i++) {
    const node = sourceTree[i];
    const parentId = node[parentIdKey];

    if (map[parentId]) {
      map[parentId].push(node);
    }
  }

  // 返回新的树结构数据
  return targetTree;
}

// 删除树某节点
function deleteNodesByProperty(tree, property, deletedNodes = []) {
  for (let i = tree.length - 1; i >= 0; i--) {
    const node = tree[i];

    if (node[property]) {
      // 将符合条件的节点从树结构中删除，并添加到删除节点数组中
      const deletedNode = tree.splice(i, 1)[0];
      deletedNodes.push(deletedNode);
    } else if (node.children) {
      // 递归处理子节点
      deleteNodesByProperty(node.children, property, deletedNodes);
    }
  }

  // 返回删除的节点和新的树结构数据
  return {
    deletedNodes,
    newTree: tree
  };
}

// 找路径
function findPathByProperty(tree, property, value, path = []) {
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];

    if (node[property] === value) {
      // 找到目标节点，将当前节点添加到路径中
      path.push(node);
      return path;
    } else if (node.children) {
      // 递归处理子节点
      const childPath = findPathByProperty(node.children, property, value, [...path, node]);
      if (childPath) {
        return childPath;
      }
    }
  }

  // 没有找到目标节点，返回空数组
  return [];
}

export {
  buildTree,
  flattenTree,
  searchTree,
  insertIntoTree,
  insertTreeIntoTree,
  deleteNodesByProperty,
  findPathByProperty
}

